# -*- coding: utf-8 -*-
# @Time: 2022/9/9 20:29
# @File: util.py
import pygame
import os
from PIL import Image
from setting import *


def image_to_animation_files(image_path):
    """
    将图片切割成多个动画文件，新建文件夹保存到animation下面
    :param image_path: 图片路径
    :return: 动画文件夹的路径
    """
    # 打开读取图片
    image_data = Image.open(image_path)
    width, height = image_data.size

    # 解析图片路径和名称中的信息，名称样例：bird_red_fly_1x8.png
    folder, image_name = os.path.split(image_path)
    full_name, suffix = image_name.split(".")
    *name, size = full_name.split("_")
    row, col = (int(i) for i in size.split("x"))

    # 判断并新建文件夹
    new_name = "_".join(name)
    new_folder = os.path.join(folder, new_name)
    if not os.path.exists(new_folder):
        os.mkdir(new_folder)

    per_width, per_height = width / col, height / row
    index = 0
    for x in range(row):
        for y in range(col):
            # 获取切割部分的左上角指标
            left, top = y * per_width, x * height
            part_image = image_data.crop(  # 图片切割
                (left, top, left + per_width, top + per_height))
            part_image_name = f"{new_name}{index}.{suffix}"
            part_image.save(os.path.join(new_folder, part_image_name))
            index += 1

    return new_folder


def get_image_pixel(image_path, location):
    """
    获取图片某坐标的RGB像素值
    :param image_path: 图片路径
    :param location: 像素坐标
    :return: RGB像素值
    """
    image_data = Image.open(image_path).convert("RGB")
    return image_data.getpixel(location)


def invert_picture(image_path, vertical=True):
    """
    对图片进行水平或垂直反转
    :param image_path: 图片路径
    :param vertical: 垂直反转，否则水平反转
    :return:
    """
    image_data = Image.open(image_path)
    if vertical:
        new_image_data = image_data.transpose(Image.FLIP_TOP_BOTTOM)
    else:
        new_image_data = image_data.transpose(Image.FLIP_LEFT_RIGHT)
    new_image_data.save(image_path)


def animation_surfaces(folder_path, is_alpha=True, background=IMAGE_BACKGROUND):
    """
    从文件夹中获取所有动画图像的对象
    :param folder_path: 动画文件夹路径
    :param is_alpha: 图像是否有透明图层
    :param background: 图片的背景色
    :return: 所有动画图像的对象
    """
    surfaces = list()
    for image_name in os.listdir(folder_path):
        image_path = os.path.join(folder_path, image_name)
        image_data = pygame.image.load(image_path)
        if is_alpha:
            surface = image_data.convert_alpha()
        else:
            surface = image_data.convert()
            surface.set_colorkey(background)
        surfaces.append(surface)

    return surfaces


def image_surface(image_path, is_alpha=True, background=IMAGE_BACKGROUND):
    """
    获取图片的对象
    :param image_path: 图片路径
    :param is_alpha: 图像是否有透明图层
    :param background: 图片的背景色
    :return: 图片对象
    """
    image_data = pygame.image.load(image_path)
    if is_alpha:
        surface = image_data.convert_alpha()
    else:
        surface = image_data.convert()
        surface.set_colorkey(background)

    return surface


if __name__ == '__main__':
    pass
